package com.yc.txx.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.yc.txx.entity.Lease;
import com.yc.txx.entity.User;
import com.yc.txx.service.BizException;
import com.yc.txx.service.ILeaseService;
import com.yc.txx.vo.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.bind.annotation.SessionAttribute;

import java.util.List;

/**
 * <p>
 * 天下行租车-用车表-10 前端控制器
 * </p>
 *
 * @author xl
 * @since 2024-05-07
 */
@RestController
@RequestMapping("/txx/lease")
public class LeaseController {

    @Autowired
    ILeaseService leaseService;

    @RequestMapping("commit")
    public Result commit(Lease lease,
                         @SessionAttribute User loginedUser) throws BizException {
        lease.setUid(loginedUser.getId());
        leaseService.commit(lease);
        return Result.ok("订单提交成功");
    }

    @RequestMapping("queryByDays")
    public List<Lease> queryByDays(String days,int cid){
        LambdaQueryWrapper<Lease> lqw = new LambdaQueryWrapper<>();
        lqw.like(Lease::getDays,days.substring(0,7) + "%");
        lqw.eq(Lease::getCid, cid);
        return leaseService.list(lqw);
    }


    @RequestMapping("query")
    public Result query(@RequestParam(defaultValue = "1") int pageNum ,
                        @RequestParam(defaultValue = "10") int pageSize,
                        @SessionAttribute User loginedUser,
                        Lease lease) throws BizException {
        //分页组件
        IPage<Lease> page = new Page<>(pageNum,pageSize);

        //注入已经登录的用户id
        lease.setUid(loginedUser.getId());

        return Result.ok("查询成功",leaseService.queryLease(lease,page));
    }
}
